import os
import cv2
import sys
import PySide6
import numpy as np
from PySide6.QtWidgets import QFileDialog, QApplication
from Denoise import denoise
from Edge import Edge
from LSD import LSD
from Binary import Binary
from Histogram import Histogram

# author: 周张珉
# 测试程序

dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

if __name__ == "__main__":
    # 文件选择器
    app = QApplication(sys.argv)
    file_path = QFileDialog.getOpenFileName(filter="图像文件(*.png *.jpg *.jpeg)")
    print(file_path[0])
    if not os.path.exists(file_path[0]):
        print("文件不存在")
        os._exit(1)
    input_pic = cv2.imdecode(np.fromfile(file_path[0], dtype=np.uint8), -1)

    # 处理
    input_pic = denoise(input_pic)
    bin = Binary.binary_pic(input_pic)
    edge = Edge.edge_detector(bin)
    lsd = LSD.LSD(input_pic)
    output_pic = cv2.addWeighted(edge, 0.5, lsd, 0.5, 100)
    Histogram.image_hist(input_pic)

    # 展示原始图片和处理后的图片
    cv2.imshow("Source", input_pic)
    cv2.imshow("Result", output_pic)
    cv2.waitKey(0)

    # 资源释放
    cv2.destroyAllWindows()
    app.exit()
